package controller

import (
	"digital/server/common"
	"digital/server/model"
	"digital/server/response"
	"fmt"
	mssql "github.com/denisenkom/go-mssqldb"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"strconv"
	"time"
)

func CreateAct(ctx *gin.Context) {
	uid := ctx.PostForm("uid") //申请创建活动的用户id
	userid, _ := strconv.Atoi(uid)
	//aid := ctx.PostForm("aid")
	//actid, _ := strconv.Atoi(aid)

	AName := ctx.PostForm("AName")
	cid := ctx.PostForm("cid")
	clubid, _ := strconv.Atoi(cid)
	Time := ctx.PostForm("CreateTime") //申请创建社团时间
	ApplyCreateTime, _ := time.Parse("2006-01-02 15:04:05", Time)

	ActTitle := AName + "创建活动申请"

	startTime1 := ctx.PostForm("startTime")
	startTime, _ := time.Parse("2006-01-02 15:04:05", startTime1)
	endTime1 := ctx.PostForm("endTime")
	endTime, _ := time.Parse("2006-01-02 15:04:05", endTime1)

	CampusStr := ctx.PostForm("campus")
	Campus, _ := strconv.Atoi(CampusStr)

	place := ctx.PostForm("place")

	AType := ctx.PostForm("type")
	ActType, _ := strconv.Atoi(AType)
	permission := ctx.PostForm("permission")
	joinPermission, _ := strconv.Atoi(permission)
	Stime1 := ctx.PostForm("stime")
	ETime1 := ctx.PostForm("etime")
	STime, _ := time.Parse("2006-01-02 15:04:05", Stime1)
	ETime, _ := time.Parse("2006-01-02 15:04:05", ETime1)

	requirement := ctx.PostForm("requirement")

	content := ctx.PostForm("content")

	File := ctx.PostForm("file")

	MID := ctx.PostForm("managerID")
	managerID, _ := strconv.Atoi(MID)

	managerName := ctx.PostForm("managerName")

	DB := common.GetDB()

	var LeaderInfo model.User
	result1 := DB.Table("users").Where("uid = ? AND uname = ?", managerID, managerName).First(&LeaderInfo)

	if result1.RowsAffected > 0 {
		// 先上传文件
		fileID := UploadFileToCOS(ctx)
		if fileID == -1 {
			response.Response(ctx, http.StatusBadRequest, 400, nil, "文件上传发生错误，请重试")
			return
		}

		activity := model.Activity{
			ATitle:      AName,
			Cid:         clubid,
			StartTime:   startTime,
			EndTime:     endTime,
			Campus:      Campus,
			Place:       place,
			Type:        ActType,
			Permission:  joinPermission,
			Stime:       STime,
			Etime:       ETime,
			Requirement: mssql.VarChar(requirement),
			Content:     mssql.VarChar(content),
			File:        mssql.VarChar(File),
			UID:         managerID, //活动负责人的id
		}

		//插入activity数据表
		if createErr := DB.Create(&activity).Error; createErr != nil {
			fmt.Println("执行 SQL 语句时发生错误:", createErr)
			response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
			return
		}

		insertAid := activity.Aid
		var applyState = 0
		activityCreate := model.ActivityCreate{
			Aid:       insertAid,
			Uid:       userid,
			ApplyTime: ApplyCreateTime,
			State:     applyState,
			File:      fileID,
		}
		//插入 申请创建活动分表
		if applyErr := DB.Create(&activityCreate).Error; applyErr != nil {
			fmt.Println("执行 SQL 语句时发生错误:", applyErr)
			response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
			return
		}

		DB1 := common.GetDB()

		//得到活动所属的社团信息
		var clubinfo model.Club

		clubResult := DB1.Table("clubs").Where("id = ?", clubid).First(&clubinfo)

		if clubResult.Error != nil {
			response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
			log.Println("err:", clubResult.Error)
			return
		}

		applyID := activityCreate.ID

		//申请总表
		applyinfo := model.Apply{
			Apply_ID:       applyID,
			UserID:         userid,
			Kind:           5,
			Approval_State: 0,
			Title:          ActTitle,
			ApplyTime:      ApplyCreateTime,
			Approver1:      &clubinfo.LeaderID, //社团负责人id
			Approver2:      &clubinfo.AdminTeacherID,
			//Approver3:, //校团委
		}

		//插入applies申请总表
		if applyErr := DB.Create(&applyinfo).Error; applyErr != nil {
			fmt.Println("执行 SQL 语句时发生错误:", applyErr)
			response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
			return
		} else {
			// response成功提示给前端
			response.Response(ctx, http.StatusOK, 200, nil, "申请创建活动成功")

		}

	} else {
		response.Success(ctx, gin.H{"state": 0}, "负责人信息不存在")
	}

}
